package com.cjkj.system.service.impl;

import com.cjkj.common.core.constant.WarnConstants;
import com.cjkj.common.core.exception.ServiceException;
import com.cjkj.system.domain.TWarnSettingHospital;
import com.cjkj.system.domain.vo.WarnSetVo;
import com.cjkj.system.mapper.TWarnSettingHospitalMapper;
import com.cjkj.system.service.ITWarnSettingHospitalService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 医院预警值设置Service业务层处理
 * 
 * @author cjkj
 * @date 2025-05-30
 */
@Service
public class TWarnSettingHospitalServiceImpl implements ITWarnSettingHospitalService 
{
    @Resource
    private TWarnSettingHospitalMapper tWarnSettingHospitalMapper;

    @Override
    public WarnSetVo getDeptWarnSet(String hospitalId, String deptId) {
        List<TWarnSettingHospital> tWarnSettingHospitals = tWarnSettingHospitalMapper.selectHospitalWarnSet(hospitalId, deptId);

        WarnSetVo vo = new WarnSetVo();
        if(tWarnSettingHospitals != null && !tWarnSettingHospitals.isEmpty())  {
            for (TWarnSettingHospital tWarnSettingHospital : tWarnSettingHospitals) {
                String warnType = tWarnSettingHospital.getWarnType();
                if(WarnConstants.WARN_TYPE_BRACELET_SPO2.equals(warnType)) {
                    vo.setSpo2(tWarnSettingHospital.getLowerLimit().longValue());
                }
                if(WarnConstants.WARN_TYPE_TEMPERATURE.equals(warnType)) {
                    vo.setTempLow(tWarnSettingHospital.getLowerLimit());
                    vo.setTempHigh(tWarnSettingHospital.getUpperLimit());
                }
                if(WarnConstants.WARN_TYPE_BPM.equals(warnType)) {
                    vo.setHrLow(tWarnSettingHospital.getLowerLimit().longValue());
                    vo.setHrHigh(tWarnSettingHospital.getUpperLimit().longValue());
                }
            }
        }
        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveDeptWarnSet(WarnSetVo req, String hospitalId, String deptId) {
        if(req == null) {
            throw new ServiceException("预警值不能为空！");
        }
        Long spo2 = req.getSpo2();
        Long hrLow = req.getHrLow();
        Long hrHigh = req.getHrHigh();
        Double tempLow = req.getTempLow();
        Double tempHigh = req.getTempHigh();

        // 删除掉之前的配置信息
        tWarnSettingHospitalMapper.deleteTWarnSets(hospitalId, deptId);

        if(spo2 == null
                && hrLow == null && hrHigh == null
                && tempLow == null && tempHigh == null) {
            return 1;
        }

        // 查询所有默认配置
        List<TWarnSettingHospital> tWarnSettingHospitals = tWarnSettingHospitalMapper.selectPublicWarnSet();

        List<TWarnSettingHospital> saveList = new ArrayList<>();
        // 血氧
        if(spo2 != null) {
            List<TWarnSettingHospital> collect = tWarnSettingHospitals
                    .stream()
                    .filter(tWarnSettingHospital -> WarnConstants.WARN_TYPE_BRACELET_SPO2.equals(tWarnSettingHospital.getWarnType()))
                    .collect(Collectors.toList());
            TWarnSettingHospital spo2Set = collect.get(0);
            spo2Set.setLowerLimit(Double.valueOf(String.valueOf(spo2)));
            saveList.add(spo2Set);
        }
        // 脉率
        if(hrLow != null || hrHigh != null) {
            List<TWarnSettingHospital> collect = tWarnSettingHospitals
                    .stream()
                    .filter(tWarnSettingHospital -> WarnConstants.WARN_TYPE_BPM.equals(tWarnSettingHospital.getWarnType()))
                    .collect(Collectors.toList());
            TWarnSettingHospital hrSet = collect.get(0);

            if(hrLow != null) {
                hrSet.setLowerLimit(Double.valueOf(String.valueOf(hrLow)));
            }
            if(hrHigh != null) {
                hrSet.setUpperLimit(Double.valueOf(String.valueOf(hrHigh)));
            }
            saveList.add(hrSet);
        }
        // 体温
        if(tempLow != null || tempHigh != null) {
            List<TWarnSettingHospital> collect = tWarnSettingHospitals
                    .stream()
                    .filter(tWarnSettingHospital -> WarnConstants.WARN_TYPE_TEMPERATURE.equals(tWarnSettingHospital.getWarnType()))
                    .collect(Collectors.toList());
            TWarnSettingHospital tempSet = collect.get(0);
            if(tempLow != null) {
                tempSet.setLowerLimit(tempLow);
            }
            if(tempHigh != null) {
                tempSet.setUpperLimit(tempHigh);
            }
            saveList.add(tempSet);
        }

        return tWarnSettingHospitalMapper.batchInsert(saveList, hospitalId, deptId);

    }

}
